package com.dz.controller.user;

import com.dz.constant.MessageConstant;
import com.dz.context.BaseContext;
import com.dz.dto.ShoppingCartDTO;
import com.dz.entity.Dish;
import com.dz.entity.ShoppingCart;
import com.dz.enumeration.AppHttpCodeEnum;
import com.dz.exception.SystemException;
import com.dz.mapper.DishMapper;
import com.dz.mapper.SetMealMapper;
import com.dz.mapper.ShoppingCartMapper;
import com.dz.result.Result;
import com.dz.service.ShoppingCartService;
import com.dz.vo.SetmealVO;
import io.jsonwebtoken.lang.Assert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @author 鄧浩奇
 * @classname ShoppingCarController
 * @description 购物车接口
 * @since 2024/4/5 15:29
 **/


@Api(tags = "购物车相关接口")
@RestController
@RequestMapping("/user/shoppingCart")
public class ShoppingCarController {

    private final DishMapper dishMapper;

    private final SetMealMapper setMealMapper;

    private final ShoppingCartService shoppingCartService;

    private final ShoppingCartMapper shoppingCartMapper;

    @Autowired
    public ShoppingCarController(DishMapper dishMapper, SetMealMapper setMealMapper, ShoppingCartService shoppingCartService, ShoppingCartMapper shoppingCartMapper) {
        this.dishMapper = dishMapper;
        this.setMealMapper = setMealMapper;
        this.shoppingCartService = shoppingCartService;
        this.shoppingCartMapper = shoppingCartMapper;
    }

    @ApiOperation("商品加入购物车")
    @PostMapping("/add")
    public Result<String> addShoppingCart(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        Assert.notNull(shoppingCartDTO, MessageConstant.PARAM_ERROR);

        // 明确是套餐还是菜品
        Long setmealId = shoppingCartDTO.getSetmealId();
        Long dishId = shoppingCartDTO.getDishId();

        ShoppingCart s = shoppingCartMapper.selectBySetmealIdOrDishId(setmealId, dishId);
        if (Objects.nonNull(s)) {
            // 判断是否是重复添加，若是则number+1; true表示number+1
            Integer row = shoppingCartMapper.updateNumber(s.getId(),true);
            return row > 0 ? Result.success() : Result.error(MessageConstant.INSERT_SHOPPING_CART_FAIL);
        }

        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart;

        if (Objects.nonNull(setmealId)) {
            // 套餐
            SetmealVO setmealVO = setMealMapper.selectById(setmealId);
            shoppingCart = ShoppingCart.builder()
                    .userId(userId)
                    .name(setmealVO.getName())
                    .image(setmealVO.getImage())
                    .setmealId(setmealId)
                    .number(1)
                    .amount(setmealVO.getPrice())
                    .createTime(LocalDateTime.now())
                    .build();

        } else if (Objects.nonNull(dishId)) {
            // 菜品
            Dish dish = dishMapper.selectDishById(dishId);
            String dishFlavor = shoppingCartDTO.getDishFlavor();
            shoppingCart = ShoppingCart.builder()
                    .userId(userId)
                    .name(dish.getName())
                    .image(dish.getImage())
                    .dishId(dishId)
                    .number(1)
                    .amount(dish.getPrice())
                    .createTime(LocalDateTime.now())
                    .build();
            if (StringUtils.hasText(dishFlavor)) {
                shoppingCart.setDishFlavor(dishFlavor);
            }
        } else throw new SystemException(AppHttpCodeEnum.ERROR_PAGE);
        Integer rows = shoppingCartService.addShoppingCart(shoppingCart);
        return rows > 0 ? Result.success() : Result.error(MessageConstant.INSERT_SHOPPING_CART_FAIL);
    }


   @ApiOperation("查询购物车")
   @GetMapping("/list")
    public Result<List<ShoppingCart>> getList() {
        Long userID = BaseContext.getCurrentId();
        List<ShoppingCart> list = shoppingCartService.getList(userID);
        return Result.success(list);
    }


    @ApiOperation("删除购物车中的一个商品")
    @PostMapping("/sub")
    public Result<String> removeOne(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        Assert.notNull(shoppingCartDTO, MessageConstant.PARAM_ERROR);

        Long setmealId = shoppingCartDTO.getSetmealId();
        Long dishId = shoppingCartDTO.getDishId();

        ShoppingCart s = shoppingCartMapper.selectBySetmealIdOrDishId(setmealId, dishId);
        Integer rows = shoppingCartService.remove(s);

        return rows > 0 ? Result.success() : Result.error(MessageConstant.DELETE_CART_FAIL);
    }


    @ApiOperation("清空购物车")
    @DeleteMapping("/clean")
    public Result<String> cleanCart() {
        Long userId = BaseContext.getCurrentId();
        Integer rows = shoppingCartService.cleanByUserId(userId);
        return rows > 0 ? Result.success() : Result.error(MessageConstant.INSERT_SHOPPING_CART_FAIL);
    }

}
